package com.wdb007.venus.svc.biz.manager;

import java.nio.charset.Charset;
import java.util.Date;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.fasterxml.jackson.databind.ObjectMapper;

@Component
public class RabbitManager {

	private Logger logger = LoggerFactory.getLogger(RabbitManager.class);
	
	@Autowired
	private RabbitTemplate rabbitTemplate;
	
	@Autowired
	private ConfigInfoManager configInfoManager;
	
	private final String topicPrefix = "/ibms/";
	
	public static final String TOPIC_EXCHANGE    = "amq.topic";
	/**系统内部传输的exchange*/
	public static final String SERVICE_EXCHANGE       = ".ibms.exchange.service";
	
	public void sendAlarm(String msg) {
		try {
			logger.info("sendAlarm------msg:{}", msg);
			logger.info("topic.exchange: {}", configInfoManager.getValue("topic.exchange"));
			logger.info("alarm.queue: {}", configInfoManager.getValue("alarm.queue"));
			rabbitTemplate.convertAndSend(configInfoManager.getValue("topic.exchange"), configInfoManager.getValue("alarm.queue"), msg.getBytes(Charset.forName("utf-8")));
		}catch(Exception e) {
			logger.error(e.getMessage(), e);
		}
	}
	
	public void sendToServiceExchange(String msg) {
		try {
			logger.info("sendToServiceExchange: {}", msg);
			rabbitTemplate.convertAndSend(SERVICE_EXCHANGE, "", msg.getBytes(Charset.forName("utf-8")));
		}catch(Exception e) {
			logger.error("向" + SERVICE_EXCHANGE +"发送消息异常", e);
		}
	}
	
	public void publish(String topic, String msg) {
        final String routeKey = topic.replaceAll("/", ".");
        sendTopic(routeKey, msg);
    }
	/**
	 * 
	 * @param did 书柜编号
	 * @param msg 给该书柜发送的消息
	 */
    public void publish(String did, Map<String, Object> msg) {
        if (msg.getOrDefault("sid", null) == null) {
            msg.put("sid", "server");//FIXME 修改serverId 为server
        }
        msg.put("did",  did);
        msg.put("chan", "mqtt");
        msg.put("ts",   new Date().getTime());
        try {
            ObjectMapper objectMapper = new ObjectMapper();
            String msgstr = objectMapper.writeValueAsString(msg);
            publish(topicPrefix + did, msgstr);
        }
        catch (Exception e) {
            logger.error(e.getMessage());
        }
    }

    public void sendTopic(String routineKey, String msg) {
        logger.debug("sendTopic: " + routineKey + " " + msg);
        try {
            MessageProperties props = new MessageProperties();
            Message message = new Message(msg.getBytes(Charset.forName("UTF-8")), props);
            rabbitTemplate.convertAndSend(TOPIC_EXCHANGE, routineKey, message);
        }
        catch (AmqpException e) {
            logger.error(e.getMessage());
        }
        catch (Exception e) {
            logger.error(e.getMessage());
        }
    }
}
